/*
 * CloudBank Mobile Knowledge Sharing for Language Learners
 * Supported by the JISC Rapid Innovation Programme #jiscri
 *
 * Copyright (C) 2010 University of Brighton
 *
 * Licensed under the GNU General Public License v3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package uk.ac.brighton.itrg.cloudbank;

public class Item
{
	// used for .toString(), .init(String)
	// as we use this with String.split(), 
	// the separator must be regex save! 
	private static final String FSEP = "::";
	
	
	// fields: direct access
	public long   _id;
	public int    flag;
	public String item;
	public String description;
	public String link;
	public String image;
	public String audio;
	public String tags;
	
	// default constructor
	public Item()
	{
		_id = -1;
		flag = 0;
		item = description = link = image = audio = tags ="";
	}
	
	public void init(Item i)
	{
		_id = i._id;
		flag = i.flag;
		item = i.item;
		description = i.description;
		link = i.link;
		image = i.image;
		audio = i.audio;
		tags = i.tags;
	}
	
	public void init(String s) throws Exception
	{
		// String.split() has the nasty habit of truncating empty elements at the end 
		// of the string, therefore the result is not guaranteed to have as may tokens 
		// as we expect, requiring additional testing.

		String toc[] = s.split(FSEP);
		int i = toc.length;
		
		if(i>0) _id = Long.parseLong(toc[0]);		else return;
		if(i>1) flag = Integer.parseInt(toc[1]);	else return;
		if(i>2) item = toc[2];						else return;
		if(i>3) description = toc[3];				else return;
		if(i>4) link = toc[4];						else return;
		if(i>5) image = toc[5];						else return;
		if(i>6) audio = toc[6];						else return;
		if(i>7) tags = toc[7];
	}

	public String toString()
	{
		StringBuilder sb = new StringBuilder();
		sb.append(Long.toString(_id)).append(FSEP)
		  .append(Integer.toString(flag)).append(FSEP)
		  .append(item).append(FSEP)
		  .append(description).append(FSEP)
		  .append(link).append(FSEP)
		  .append(image).append(FSEP)
		  .append(audio).append(FSEP)
		  .append(tags);
		return sb.toString();
	}
}
